import 'package:flutter/material.dart';
import 'package:play_android/com/zhihaoliang/const/AppColors.dart';
import 'package:play_android/com/zhihaoliang/const/AppStr.dart';
import 'package:play_android/com/zhihaoliang/play/search/SearchPage.dart';

import 'find/view/FindPage.dart';
import 'home/view/HomePage.dart';
import 'mine/view/MinePage.dart';

///  ***************************************************************************
///  应用的第一个页面主页
///  ***************************************************************************

class PlayApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _PlayAppState();
  }
}

class _PlayAppState extends State<PlayApp> {
  ///用于控制 home_page、find_page和mine_page之间的跳转
  int _tabIndex = 0;

  ///底部的导航View
  List<BottomNavigationBarItem> _navViews = <BottomNavigationBarItem>[
    BottomNavigationBarItem(
      icon: Icon(Icons.home),
      title: Text(AppStr.navTitles[0]),
      backgroundColor: Colors.blue,
    ),
    BottomNavigationBarItem(
      icon: Icon(Icons.widgets),
      title: Text(AppStr.navTitles[1]),
      backgroundColor: Colors.blue,
    ),
    BottomNavigationBarItem(
      icon: Icon(Icons.person),
      title: Text(AppStr.navTitles[2]),
      backgroundColor: Colors.blue,
    ),
  ];

  /// 没有Tab需要展示的页面
  IndexedStack _indexedStack;

  void initData() {
    _indexedStack = IndexedStack(
      children: <Widget>[HomePage(), FindPage(), MinePage()],
      index: _tabIndex,
    );
  }

  @override
  void initState() {
    initData();
    super.initState();
  }

  ///总体的key
  final GlobalKey<NavigatorState> _navKey = GlobalKey<NavigatorState>();

  @override
  Widget build(BuildContext context) {
    initData();
    return MaterialApp(
      navigatorKey: _navKey,
      theme: ThemeData(
          primaryColor: AppColors.colorPrimary, accentColor: Colors.blue),
      home: Scaffold(
        appBar: AppBar(
          title: Text(
            AppStr.navTitles[_tabIndex],
            style: TextStyle(color: Colors.white),
          ),
          actions: <Widget>[
            IconButton(
              icon: IconButton(
                icon: Icon(Icons.search),
                onPressed: () {
                  _navKey.currentState.push(
                      MaterialPageRoute(builder: (context) => SearchPage()));
                },
              ),
            ),
          ],
        ),
        body: _indexedStack,
        bottomNavigationBar: BottomNavigationBar(
          items: _navViews
              .map((BottomNavigationBarItem _navViews) => _navViews)
              .toList(),
          currentIndex: _tabIndex,
          type: BottomNavigationBarType.fixed,
          onTap: (index) {
            setState(() {
              _tabIndex = index;
            });
          },
        ),
      ),
    );
  }
}
